<?php
$adm_menu = "1";

include("config.php");
if (!isset($_SESSION["auser"]) || $_SESSION["auser"] == "" || $_SESSION['agroup'] != "WEB_ADMIN") header('Location: index.php');


$db_www = new DB($server_username,$server_pass,$server_db,$server,$driver);
$db_rdata = new DB($rdata_server_username,$rdata_server_pass,$rdata_server_db,$rdata_server,$rdata_driver);


if (isset($_REQUEST["kodas"])) {
	$kodas = $_REQUEST["kodas"];
} else {
	$kodas = "";
}

$err = "";
if (isset($_REQUEST["act"])){
	if ($_REQUEST["act"] == "del" && isset($_REQUEST["id"])) {
	
	//pirmiausiai patikrinam ar nera protokolo irasu su sita preke
				
		$test_q = "SELECT count(*) as cnt FROM [x02_prot] WHERE [X02_kodas_ps] = '".addslashes_mssql($_REQUEST["id"])."'";	
		$im = $db_rdata->returnInfo($test_q);	
		if ($im['cnt']==0) {	
			$db_rdata->execute("exec web_del_preke @id ='".addslashes_mssql($_REQUEST["id"])."'");	
		} else {
		    $err .= "Trinti negalima, nes prekei egzistuoja protokolo įrašai!";
		} 
	} else if ($_REQUEST["act"] == "save" ) {
		//patikrinam ar tokia preke dar nera sukurta
		$test_q = "SELECT count(*) as cnt FROM [x01_prot_n] WHERE [X01_kodas_ps] = '".addslashes_mssql($_REQUEST["kodas"])."'";	
		$im = $db_rdata->returnInfo($test_q);	
		if ($im['cnt']>0) {	
			$err .= "Prekė tokiu kodu jau egzistuoja!";
		} else {
			//patikrinam ar tokia preke egzistuoja	
			$sql = "select top 1 N37_TRUM_PAV from n37_pmat where cast(n37_bar_kodas as numeric(12,0))='".addslashes_mssql($_REQUEST["kodas"])."'";
			$bar = $db_www->returnInfo($sql);
			if (empty($bar)) {
				$err .= "Nerasta prekė tokiu kodu!";
			} else {
				$sql = "exec web_insert_preke "
				. "@X01_kodas_ps='".addslashes_mssql($_REQUEST["kodas"])."',"
				. "@X01_pav='".addslashes_mssql(iconv('windows-1257', 'UTF-8',$bar['N37_TRUM_PAV']))."'";
				
				$db_rdata->execute($sql);
				$kodas = "";			
			}
		}

	}
}
	
include("header.php");
include("menu.php");





			
?>


<table class="innercont" cellspacing="0" cellpadding="10" border="0">
<tr><td class="leftcol2">




</td><td class="rightcol" colspan="2">


<table width="100%" border="0">

<tr>
<td valign="top">


</td>
</tr>

<tr><td valign="top">
<?php
if (isset($err) && $err != ""){
	echo "<p style='background-color:transparent;color:red;font-weight:bold;font-size:10px;'>".$err."</p>";
}
?>
<script>
function delItem(id){
	var answer = confirm("Ar ištrinti prekę?");
	if (answer){
		window.location='protocol_items.php?act=del&id='+id ;
	}

}
</script>
<form method="post" name='forma' action='protocol_items.php?act=save'>

<table cellspacing="0" cellpadding="5" align="left" class="innercont3">
<tr>
<th>Prekės kodas</th><th>Pavadinimas</th><th></th></tr>
<?php
$sql = "SELECT * FROM [x01_prot_n] ";

$list = $db_rdata->returnList($sql);
foreach ($list as $item) {
	echo "<tr>";
	echo "<td>".htmlspecialchars($item["X01_kodas_ps"])."</td>";

	echo "<td>".htmlspecialchars($item["X01_pav"])."</td>";
	echo "<td class='click' onclick=\"delItem('".$item["X01_kodas_ps"]."');return false;\"><img src='img/delete.jpg' width='11' alt='Trinti' title='Trinti'></td>";
	echo "</tr>";
}

?>
<tr><td><input class="inp" type='text' name="kodas" style="width:80px;" value="<?php echo htmlspecialchars($kodas); ?>"><br>
</td>
<td valign="top"></td>
<td valign="top"><input class="but bgwhite nopad" type="submit" value="Išsaugoti"></td></tr>
</table>
</form>
</td></tr>
</table>



</td></tr>

<tr><td>&nbsp;</td><td>

<Button id='unblockButton' class='but bgwhite nopad' onclick="window.location='<?php echo "aprotocol.php";?>';return false;">Atgal</Button>

<?php

?>
</td>
<td style="text-align:right;">

</td>
</tr>
</table>

<?php
include("footer.php");
?>